#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 1e6 + 10, INF = 0x3f3f3f3f3f3f3f3f;

ll n, m;
struct node {
    ll x, y, w;
}edge[N];

struct DSU {
    std::vector<ll> f, sz, mi;
    DSU() {}
    DSU(ll n) { init(n); }
    void init(ll n) {
        n++;
        mi.resize(n);
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        sz.assign(n, 1);
        std::iota(mi.begin(), mi.end(), INF);
    }
    ll find(ll x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    ll size(ll x) {
        return sz[find(x)];
    }
    bool merge(ll x, ll y, ll w) {
        x = find(x);
        y = find(y);
        if (x == y) return false;
        mi[x] = min({ mi[x], mi[y],w });
        sz[x] += sz[y];
        f[y] = x;
        return true;
    }
    bool same(ll x, ll y) {
        return find(x) == find(y);
    }
    ll qry(ll x) {
        return mi[find(x)];
    }
};

void solve()
{
    cin >> n >> m;
    for (ll i = 1;i <= m;i++) cin >> edge[i].x >> edge[i].y >> edge[i].w;
    sort(edge + 1, edge + m + 1, [](node a, node b) {return a.w < b.w;});

    DSU dsu(n);
    ll ans = INF;
    for (ll i = 1;i <= m;i++) {
        ll x = edge[i].x, y = edge[i].y, w = edge[i].w;
        dsu.merge(x, y, w);
        if (dsu.same(1, n)) ans = min(ans, w + dsu.qry(1));
    }
    cout << ans << '\n';
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    cin >> T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}